An OPEN-SOURCE programming LANGUAGE and free software ENVIRONMENT for STATISTICAL COMPUTING and GRAPHICS
> R uses a ...
> command line interface |
"R doesn’t protect you from yourself: you can easily shoot yourself in the foot. As long as you don’t aim the gun at your foot and pull the trigger, you won’t have a problem."
Grammar for Data Manipulation
library(dplyr)
mtcars %>%
filter(mpg >= 15) %>%
group_by(cyl) %>%
summarise(numCARS = n(),
avgMPG = mean(mpg),
avgHP = mean(hp),
medWT = median(wt),
pctMANUAL = mean(am)) %>%
arrange(cyl)
# A tibble: 3 × 6
cyl numCARS avgMPG avgHP medWT pctMANUAL
<dbl> <int> <dbl> <dbl> <dbl> <dbl>
1 4 11 26.66364 82.63636 2.200 0.7272727
2 6 7 19.74286 122.28571 3.215 0.4285714
3 8 9 16.47778 198.77778 3.570 0.2222222
Publication Quality Graphics
library(ggplot2)
ggplot(data = mtcars, aes(x = hp, y = mpg)) +
geom_point() +
stat_smooth(method = lm)
Dynamic Documents, Presentations and Reports
Interactive HTML Maps
library(leaflet)
leaflet() %>%
addTiles() %>%
setView(lng = -81.6925,
lat = 41.50132,
zoom = 17) %>%
addMarkers(lng = -81.695174,
lat = 41.501313,
popup = paste0("<b>HIMSS Innovation Center</b>",
"<br>4th floor of the Global Center for Health Innovation",
"<br>1 St Clair Ave NE",
"<br>Cleveland, OH 44114"))
Interactive HTML Tables
library(DT)
datatable(iris,
extensions = "Scroller",
options = list(
scrollY = 320,
scrollCollapse = TRUE
),
rownames = FALSE)
Interactive HTML Time Series Plots
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths) %>%
dyLegend(width = 300,
show = "always") %>%
dyRangeSelector(dateWindow = c("1974-01-01",
"1979-12-31"))
ODBC Database Access
library(RODBC)
ch <- odbcConnect("Adhoc")
x <- sqlQuery(ch, "select * from dbo.MyTable;")
close(ch)
head(x)
id random1 random2 1 1 0.27246300 0.9990163 2 2 0.48424002 0.7186590 3 3 0.63359538 -0.4202009 4 4 0.00975549 1.4590855 5 5 0.75757926 -0.4867805 6 6 0.42360277 0.5008907
Book1.xlsx
Excel Connector for R
library(XLConnect)
x <- readWorksheetFromFile(file = "../xlsx/Book1.xlsx",
sheet = "Sheet1",
startRow = 2,
startCol = 2,
endRow = 3,
endCol = 3,
header = FALSE)
y <- x + 4
wb <- loadWorkbook("../xlsx/Book1.xlsx")
setStyleAction(object = wb,
type = XLC$"STYLE_ACTION.NONE")
writeWorksheet(object = wb,
data = y,
sheet = "Sheet1",
startRow = 5,
startCol = 4,
header = FALSE)
saveWorkbook(wb)